<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">
    <title>统计信息 - 会议室预订系统</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
            padding: 30px;
            position: relative;
            overflow: hidden;
        }

        .container::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.1), transparent);
            transform: rotate(45deg);
            z-index: -1;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        .header h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }

        .header p {
            color: #666;
            font-size: 16px;
        }

        .stats-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 25px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            transition: transform 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-5px);
        }

        .stat-header {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-bottom: 20px;
        }

        .stat-icon {
            width: 50px;
            height: 50px;
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
        }

        .icon-blue {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .icon-green {
            background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%);
            color: white;
        }

        .icon-purple {
            background: linear-gradient(135deg, #9b59b6 0%, #8e44ad 100%);
            color: white;
        }

        .stat-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
        }

        .stat-value {
            font-size: 32px;
            font-weight: 700;
            color: #333;
            margin-top: 10px;
        }

        .chart-container {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
            margin-bottom: 30px;
        }

        .chart-header {
            font-size: 20px;
            margin-bottom: 20px;
            color: #333;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .chart-wrapper {
            position: relative;
            height: 400px;
            width: 100%;
        }

        .loading {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 400px;
            font-size: 18px;
            color: #666;
        }

        .notification {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 15px 25px;
            border-radius: 8px;
            color: white;
            font-weight: 500;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            z-index: 2000;
            display: none;
            animation: slideIn 0.3s ease;
        }

        .notification.success {
            background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%);
        }

        .notification.error {
            background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
        }

        @keyframes slideIn {
            from {
                transform: translateX(100%);
                opacity: 0;
            }
            to {
                transform: translateX(0);
                opacity: 1;
            }
        }

        /* 返回链接容器 */
        .back-link-container {
            margin-bottom: 20px;
            position: relative;
            z-index: 1;
        }

        /* 返回链接样式 */
        .back-link {
            display: inline-flex;
            align-items: center;
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
            padding: 8px 16px;
            border-radius: 8px;
        }

        .back-link:hover {
            background: rgba(102, 126, 234, 0.1);
            text-decoration: none;
            transform: translateX(-3px);
        }

        @media (max-width: 768px) {
            .stats-container {
                grid-template-columns: 1fr;
            }
            
            .chart-wrapper {
                height: 300px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="back-link-container">
            <a href="/api/major.html" class="back-link">
                <i class="fas fa-arrow-left"></i> 返回主页
            </a>
        </div>
        
        <div class="header">
            <h1><i class="fas fa-chart-bar"></i> 统计信息</h1>
            <p>查看会议室预订统计信息</p>
        </div>
        
        <div class="stats-container">
            <div class="stat-card">
                <div class="stat-header">
                    <div class="stat-icon icon-blue">
                        <i class="fas fa-door-open"></i>
                    </div>
                    <div class="stat-title">总会议室数</div>
                </div>
                <div class="stat-value" id="totalRooms">0</div>
            </div>
            
            <div class="stat-card">
                <div class="stat-header">
                    <div class="stat-icon icon-green">
                        <i class="fas fa-calendar-check"></i>
                    </div>
                    <div class="stat-title">本周预订数</div>
                </div>
                <div class="stat-value" id="totalBookings">0</div>
            </div>
            
            <div class="stat-card">
                <div class="stat-header">
                    <div class="stat-icon icon-purple">
                        <i class="fas fa-chart-line"></i>
                    </div>
                    <div class="stat-title">最热门会议室</div>
                </div>
                <div class="stat-value" id="popularRoom">-</div>
            </div>
        </div>
        
        <div class="chart-container">
            <h2 class="chart-header"><i class="fas fa-chart-pie"></i> 会议室使用统计</h2>
            <div class="chart-wrapper">
                <canvas id="roomUsageChart"></canvas>
            </div>
        </div>
        
        <div class="chart-container">
            <h2 class="chart-header"><i class="fas fa-chart-line"></i> 本周预订趋势</h2>
            <div class="chart-wrapper">
                <canvas id="bookingTrendChart"></canvas>
            </div>
        </div>
    </div>
    
    <!-- 通知 -->
    <div id="notification" class="notification"></div>
    
    <script>
        // 图表实例
        let roomUsageChart = null;
        let bookingTrendChart = null;
        
        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            loadStatistics();
            loadMeetingRooms();
        });
        
        // 加载统计信息
        function loadStatistics() {
            fetch('/api/meetings/statistics/weekly')
                .then(response => response.json())
                .then(data => {
                    if (data.code === 200) {
                        updateStatistics(data.data);
                        renderCharts(data.data);
                    } else {
                        showNotification('加载统计信息失败: ' + data.message, 'error');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    showNotification('加载统计信息时发生错误', 'error');
                });
        }
        
        // 加载会议室列表
        function loadMeetingRooms() {
            fetch('/api/meetings/rooms')
                .then(response => response.json())
                .then(data => {
                    if (data.code === 200) {
                        document.getElementById('totalRooms').textContent = data.data.length;
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                });
        }
        
        // 更新统计信息
        function updateStatistics(stats) {
            // 计算总预订数
            let totalBookings = 0;
            let maxBookings = 0;
            let popularRoom = '-';
            
            // 计算总预订数和最热门会议室
            for (const [room, count] of Object.entries(stats.roomStats || {})) {
                totalBookings += count;
                if (count > maxBookings) {
                    maxBookings = count;
                    popularRoom = room;
                }
            }
            
            document.getElementById('totalBookings').textContent = totalBookings;
            document.getElementById('popularRoom').textContent = popularRoom;
        }
        
        // 渲染图表
        function renderCharts(stats) {
            // 渲染会议室使用统计图
            renderRoomUsageChart(stats.roomStats || {});
            
            // 渲染预订趋势图
            renderBookingTrendChart(stats.dailyStats || {});
        }
        
        // 渲染会议室使用统计图
        function renderRoomUsageChart(roomStats) {
            const ctx = document.getElementById('roomUsageChart').getContext('2d');
            
            // 如果已有图表实例，先销毁
            if (roomUsageChart) {
                roomUsageChart.destroy();
            }
            
            // 准备数据
            const labels = Object.keys(roomStats);
            const data = Object.values(roomStats);
            
            // 创建图表
            roomUsageChart = new Chart(ctx, {
                type: 'pie',
                data: {
                    labels: labels,
                    datasets: [{
                        data: data,
                        backgroundColor: [
                            '#667eea',
                            '#764ba2',
                            '#f093fb',
                            '#f5576c',
                            '#4facfe',
                            '#00f2fe',
                            '#43e97b',
                            '#38f9d7',
                            '#fa709a',
                            '#fee140'
                        ],
                        borderWidth: 2,
                        borderColor: '#fff'
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    plugins: {
                        legend: {
                            position: 'right',
                        },
                        tooltip: {
                            callbacks: {
                                label: function(context) {
                                    const label = context.label || '';
                                    const value = context.parsed || 0;
                                    const total = context.dataset.data.reduce((a, b) => a + b, 0);
                                    const percentage = Math.round((value / total) * 100);
                                    return `${label}: ${value} (${percentage}%)`;
                                }
                            }
                        }
                    }
                }
            });
        }
        
        // 渲染预订趋势图
        function renderBookingTrendChart(dailyStats) {
            const ctx = document.getElementById('bookingTrendChart').getContext('2d');
            
            // 如果已有图表实例，先销毁
            if (bookingTrendChart) {
                bookingTrendChart.destroy();
            }
            
            // 准备数据
            const labels = Object.keys(dailyStats).map(date => {
                const d = new Date(date);
                return `${d.getMonth() + 1}-${d.getDate()}`;
            });
            
            const data = Object.values(dailyStats);
            
            // 创建图表
            bookingTrendChart = new Chart(ctx, {
                type: 'line',
                data: {
                    labels: labels,
                    datasets: [{
                        label: '预订数量',
                        data: data,
                        borderColor: '#667eea',
                        backgroundColor: 'rgba(102, 126, 234, 0.1)',
                        borderWidth: 3,
                        pointBackgroundColor: '#764ba2',
                        pointRadius: 5,
                        pointHoverRadius: 7,
                        fill: true,
                        tension: 0.3
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    plugins: {
                        legend: {
                            display: false
                        },
                        tooltip: {
                            mode: 'index',
                            intersect: false
                        }
                    },
                    scales: {
                        y: {
                            beginAtZero: true,
                            ticks: {
                                precision: 0,
                                stepSize: 1
                            },
                            title: {
                                display: true,
                                text: '预订数量'
                            }
                        },
                        x: {
                            title: {
                                display: true,
                                text: '日期'
                            }
                        }
                    }
                }
            });
        }
        
        // 显示通知
        function showNotification(message, type) {
            const notification = document.getElementById('notification');
            notification.textContent = message;
            notification.className = 'notification ' + type;
            notification.style.display = 'block';
            
            setTimeout(() => {
                notification.style.display = 'none';
            }, 3000);
        }
    </script>
</body>
</html>